﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetOpenAuth.OpenId;
using DotNetOpenAuth;
using DotNetOpenAuth.Messaging;
using DotNetOpenAuth.OpenId.RelyingParty;
using DotNetOpenAuth.OpenId.Extensions.AttributeExchange;

namespace _1041345_1041368_1041422_DoAnCuoiKy.Pages
{
    public partial class Site : System.Web.UI.MasterPage
    {
        public static int flag = 0;
        protected void Page_Load(object sender, EventArgs e)
        {           
            OpenIdRelyingParty rp = new OpenIdRelyingParty();            
            var response = rp.GetResponse();
            if (response != null)
            {
                switch (response.Status)
                {
                    case AuthenticationStatus.Authenticated:
                        if (flag == 1)
                        {
                            String []str = response.ClaimedIdentifier.ToString().Split('/');
                            String[] temp = str[4].Split('#');
                            Session["Identifier"] = temp[0].ToString();                            
                        }
                        else if (flag == 2)
                        {
                            String[] str = response.ClaimedIdentifier.ToString().Split('=');
                            Session["Identifier"] = str[1];
                        }
                        break;
                    case AuthenticationStatus.Canceled:
                        Session["Identifier"] = null;                        
                        break;
                    case AuthenticationStatus.Failed:
                        Session["Identifier"] = null;;                      
                        break;
                }
            }
        }

        public void Handle(String str)
        {
            string discoveryUri = str;
            OpenIdRelyingParty openid = new OpenIdRelyingParty();
            var URIbuilder = new UriBuilder(Request.Url) { Query = "" };
            var req = openid.CreateRequest(discoveryUri, URIbuilder.Uri, URIbuilder.Uri);
            req.RedirectToProvider();
        }

        protected void btnSignInGoogle_Command(object sender, CommandEventArgs e)
        {
            if (flag == 0)
            {
                flag = 2;
                Handle(e.CommandArgument.ToString());
            }
        }        

        protected void btnLogOut_Command(object sender, CommandEventArgs e)
        {
            if (flag == 1)
            {
                flag = 0;                
                Response.Redirect("http://login.yahoo.com/config/login?logout=1");               
            }
            else if (flag == 2)
            {
                flag = 0;                
                Response.Redirect("http://accounts.google.com/logout");
            }            
        }

        protected void hlSignInYahoo_Command(object sender, CommandEventArgs e)
        {
            if (flag == 0)
            {
                flag = 1;
                Handle(e.CommandArgument.ToString());
            }
        }       

        protected void btnHome_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("~/Pages/Home.aspx");
        }       

        protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
        {
            if (e.Day.IsOtherMonth)            
                e.Cell.Text = "";           
        }

        protected void btnMyPage_Click(object sender, ImageClickEventArgs e)
        {
            if (Session["Identifier"] == null)
                Response.Redirect("~/Pages/Home.aspx");
            else
                Response.Redirect("~/Pages/Individual.aspx?ID="+ Session["Identifier"].ToString());
        }                                
    }
}